home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 30 / Mac Magazin and MacEasy Magazine CD - Issue 30.iso / utilities / Mac OS X / Flurry / Flurry source / Source Folder / Particle.cpp < prev    next >
C/C++ Source or Header  |  2001-12-13  |  5KB  |  203 lines

  1. // Particle.cpp: implementation of the Particle class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "std.h"
  6. #include "Particle.h"
  7. #define MAXANGLES 16384
  8. extern int theTexture;
  9.  
  10. // used to compute the min and max of two expresions
  11. #define MIN(a, b)  (((a) < (b)) ? (a) : (b)) 
  12. #define MAX(a, b)  (((a) > (b)) ? (a) : (b)) 
  13.  
  14. inline float FastDistance2D(float x, float y)
  15. {
  16.     // this function computes the distance from 0,0 to x,y with ~3.5% error
  17.     
  18.     // first compute the absolute value of x,y
  19.     x = (x < 0.0f) ? -x : x;
  20.     y = (y < 0.0f) ? -y : y;
  21.     
  22.     // compute the minimum of x,y
  23.     float mn = MIN(x,y);
  24.     
  25.     // return the distance
  26.     return(x+y-(mn*0.5f)-(mn*0.25f)+(mn*0.0625f));
  27.     
  28. } // end FastDistance2D
  29.  
  30. //////////////////////////////////////////////////////////////////////
  31. // Construction/Destruction
  32. //////////////////////////////////////////////////////////////////////
  33.  
  34. Particle::Particle()
  35. {
  36.     Init();
  37. }
  38.  
  39. Particle::~Particle()
  40. {
  41.  
  42. }
  43.  
  44. void Particle::Draw() // the math was easier in 2D - so 2D it is
  45. {
  46.     static int polylist = 0;
  47.     // near clip
  48.     if (z < 100.0f)
  49.     {
  50.         Init();
  51.         return;
  52.     }
  53.     float screenx = (x * sys_glWidth / z) + sys_glWidth * 0.5f;
  54.     // side clip
  55.     if (screenx > sys_glWidth + 100.0f || screenx < -100.0f)
  56.     {
  57.         Init();
  58.         return;
  59.     }
  60.     float screeny = (y * sys_glWidth / z) + sys_glHeight * 0.5f;
  61.     // vertical clip
  62.     if (screeny > sys_glHeight + 100.0f || screeny < -100.0f)
  63.     {
  64.         Init();
  65.         return;
  66.     }
  67.     
  68.     float oldscreenx = (oldx * sys_glWidth / oldz) + sys_glWidth * 0.5f;
  69.     float oldscreeny = (oldy * sys_glWidth / oldz) + sys_glHeight * 0.5f;
  70.     
  71. //    glColor4f(r,g,b,1.0f);
  72.     starfieldColor[starfieldColorIndex++] = r;
  73.     starfieldColor[starfieldColorIndex++] = g;
  74.     starfieldColor[starfieldColorIndex++] = b;
  75.     starfieldColor[starfieldColorIndex++] = 1.0f;
  76.     starfieldColor[starfieldColorIndex++] = r;
  77.     starfieldColor[starfieldColorIndex++] = g;
  78.     starfieldColor[starfieldColorIndex++] = b;
  79.     starfieldColor[starfieldColorIndex++] = 1.0f;
  80.     starfieldColor[starfieldColorIndex++] = r;
  81.     starfieldColor[starfieldColorIndex++] = g;
  82.     starfieldColor[starfieldColorIndex++] = b;
  83.     starfieldColor[starfieldColorIndex++] = 1.0f;
  84.     starfieldColor[starfieldColorIndex++] = r;
  85.     starfieldColor[starfieldColorIndex++] = g;
  86.     starfieldColor[starfieldColorIndex++] = b;
  87.     starfieldColor[starfieldColorIndex++] = 1.0f;
  88.     
  89.     float dx = (screenx-oldscreenx);
  90.     float dy = (screeny-oldscreeny);
  91.     
  92.     float m = FastDistance2D(dx, dy);// * 0.75f;
  93.     
  94.     animFrame++;
  95.     if (animFrame == 64)
  96.     {
  97.         animFrame = 0;
  98.     }
  99.     float u0 = (animFrame&&7) * 0.125f;
  100.     float v0 = (animFrame>>3) * 0.125f;
  101.     float u1 = u0 + 0.125f;
  102.     float v1 = v0 + 0.125f;
  103.     
  104.     float size = (3500.0f*(sys_glWidth/1024.0f));
  105.     
  106.     float w = max(1.5f,size/z);
  107.     float ow = max(1.5f,size/oldz);
  108.     
  109.     float d = FastDistance2D(dx, dy);
  110.     
  111.     float s;
  112.     if (d)
  113.     {
  114.         s = w/d;
  115.     }
  116.     else
  117.     {
  118.         s = 0.0f;
  119.     }
  120.     float os;
  121.     if (d)
  122.     {
  123.         os = ow/d;
  124.     }
  125.     else
  126.     {
  127.         os = 0.0f;
  128.     }
  129.     
  130.     m = 2.0f + s; 
  131.     
  132.     float dxs = dx*s;
  133.     float dys = dy*s;
  134.     float dxos = dx*os;
  135.     float dyos = dy*os;
  136.     float dxm = dx*m;
  137.     float dym = dy*m;
  138.         
  139. //    glBegin(GL_TRIANGLE_STRIP);
  140. //    glTexCoord2f(u0,v0);
  141.     starfieldTextures[starfieldTexturesIndex++] = u0;
  142.     starfieldTextures[starfieldTexturesIndex++] = v0;
  143. //    glVertex2f(screenx+dxm-dys,screeny+dym+dxs);
  144.     starfieldVertices[starfieldVerticesIndex++] = screenx+dxm-dys;
  145.     starfieldVertices[starfieldVerticesIndex++] = screeny+dym+dxs;
  146. //    glTexCoord2f(u0,v1);
  147.     starfieldTextures[starfieldTexturesIndex++] = u0;
  148.     starfieldTextures[starfieldTexturesIndex++] = v1;
  149. //    glVertex2f(screenx+dxm+dys,screeny+dym-dxs);
  150.     starfieldVertices[starfieldVerticesIndex++] = screenx+dxm+dys;
  151.     starfieldVertices[starfieldVerticesIndex++] = screeny+dym-dxs;
  152. //    glTexCoord2f(u1,v1);
  153.     starfieldTextures[starfieldTexturesIndex++] = u1;
  154.     starfieldTextures[starfieldTexturesIndex++] = v1;
  155. //    glVertex2f(oldscreenx-dxm+dyos,oldscreeny-dym-dxos);
  156.     starfieldVertices[starfieldVerticesIndex++] = oldscreenx-dxm+dyos;
  157.     starfieldVertices[starfieldVerticesIndex++] = oldscreeny-dym-dxos;
  158. //    glTexCoord2f(u1,v0);
  159.     starfieldTextures[starfieldTexturesIndex++] = u1;
  160.     starfieldTextures[starfieldTexturesIndex++] = v0;
  161. //    glVertex2f(oldscreenx-dxm-dyos,oldscreeny-dym+dxos);
  162.     starfieldVertices[starfieldVerticesIndex++] = oldscreenx-dxm-dyos;
  163.     starfieldVertices[starfieldVerticesIndex++] = oldscreeny-dym+dxos;
  164. //    glEnd();
  165. }
  166.  
  167. void Particle::Update()
  168. {
  169.     oldx = x;
  170.     oldy = y;
  171.     oldz = z;
  172.  
  173.     x += deltax*fDeltaTime;
  174.     y += deltay*fDeltaTime;
  175.     z += deltaz*fDeltaTime;
  176. }
  177.  
  178. void Particle::Init()
  179. {
  180. //    float tempx,tempy;
  181.     int r1,r2;
  182.     oldz = RandFlt(2500.0f,22500.0f);
  183. //    do
  184. //    {
  185.         r1 = rand();
  186.         r2 = rand();
  187.         oldx = ((float) (r1 % (int) sys_glWidth) - sys_glWidth * 0.5f) / (sys_glWidth / oldz);
  188.         oldy = (sys_glHeight * 0.5f - (float) (r2 % (int) sys_glHeight)) / (sys_glWidth / oldz);
  189. //        tempx = (oldx * sys_glWidth / 75.0f) + sys_glWidth/2.0f;
  190. //        tempy = (oldy * sys_glWidth / 75.0f) + sys_glHeight/2.0f;
  191. //    } while (fabs(tempx) < sys_glWidth + 100.0 && fabs(tempy) < sys_glHeight + 100.0);
  192.     deltax = 0.0f;
  193.     deltay = 0.0f;
  194.     deltaz = (float) -starSpeed;
  195.     x = oldx + deltax;
  196.     y = oldy + deltay;
  197.     z = oldz + deltaz;
  198.     r = RandFlt(0.125f,1.0f);
  199.     g = RandFlt(0.125f,1.0f);
  200.     b = RandFlt(0.125f,1.0f);
  201.     animFrame = 0;
  202. }
  203.